package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.School;
import com.yunji.news.mapper.SchoolMapper;
import com.yunji.news.service.SchoolService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 院校服务实现类
 *
 * @author yunji
 * @since 2024-11-23
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {
    
    @Override
    public IPage<School> getSchoolPage(Integer page, Integer size, String keyword, String city, String schoolType) {
        Page<School> pageParam = new Page<>(page, size);
        return baseMapper.selectSchoolPage(pageParam, keyword, city, schoolType);
    }
    
    @Override
    public List<School> getSchoolLogos() {
        return baseMapper.selectSchoolLogos();
    }
    
    @Override
    public School getBySchoolCode(String schoolCode) {
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(School::getSchoolCode, schoolCode);
        return getOne(wrapper);
    }
    
    @Override
    public boolean createSchool(School school) {
        // 检查院校代码是否已存在
        if (getBySchoolCode(school.getSchoolCode()) != null) {
            throw new RuntimeException("院校代码已存在");
        }
        return save(school);
    }
    
    @Override
    public boolean updateSchool(School school) {
        // 检查院校代码是否已被其他院校使用
        School existSchool = getBySchoolCode(school.getSchoolCode());
        if (existSchool != null && !existSchool.getId().equals(school.getId())) {
            throw new RuntimeException("院校代码已存在");
        }
        return updateById(school);
    }
    
    @Override
    public boolean deleteSchool(Long id) {
        return removeById(id);
    }
    
    @Override
    public boolean updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<School> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(School::getId, id)
               .set(School::getStatus, status);
        return update(wrapper);
    }
    
    @Override
    public List<School> getSchoolRanking(Integer limit) {
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(School::getStatus, 1)
               .eq(School::getDeleted, 0)
               .orderByAsc(School::getSortOrder)
               .orderByDesc(School::getCreatedAt)
               .last("LIMIT " + limit);
        return list(wrapper);
    }
    
    @Override
    public List<School> getSchoolPhones() {
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(School::getStatus, 1)
               .eq(School::getDeleted, 0)
               .isNotNull(School::getPhone)
               .ne(School::getPhone, "")
               .select(School::getId, School::getName, School::getShortName, School::getPhone)
               .orderByAsc(School::getSortOrder);
        return list(wrapper);
    }
}